package Control;

import java.sql.ResultSet;
import java.sql.SQLException;

public class User_CommonControls {
	
	public boolean ValidationPwd(String oldPwd, String newPwd){
		boolean success = false;
		
		if (oldPwd == newPwd)
			success = true;
		
		return success;
	}
	
	public static boolean ValidationContactNo(String ContactNo){
		boolean success = false;
		
		char tempCheck;
		
		//if length is not 8 return false
		if (ContactNo.length() != 8){
			success = false;
		}
		else{
			//for loop to check that all entered are num and not letters
			int intTempCheck;
			for (int i = 0; i < 8; i++){
				tempCheck = ContactNo.charAt(i);
				intTempCheck = Character.getNumericValue(tempCheck);
				
					if (intTempCheck >= 0 && intTempCheck <= 9){
						success = true;
						}
					else{
						success = false;
					}
				}
					
			}
		
		return success;
	}
	
	public static boolean ValidationMonthlyIncome(double d){
		boolean success = false;
		
		// if monthly income is between 0 and 1500 return true
		if (d >= 0 && d <= 3000){
			success = true;
		}
		
		return success;
	}
	
	public static boolean ValidationFamilySize(int FamilySize){
		boolean success = false;
		
		//If family size is from 0 to 20 return true
		if (FamilySize >= 0 && FamilySize <= 20){
			success = true;
		}
		
		return success;
	}
	
	public static boolean ValidateGender(String gender){
		boolean success = false;
		char Gender = gender.charAt(0);
		
		
		if (Gender != 'M' && Gender != 'F'){
			success = false;
		}
		else success = true;
		
		return success;
	}
	
	public static boolean CheckDuplicate(String nric){
		boolean success = false;
		String NRIC = null;
		
		ResultSet rs = null;
		
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "SELECT * FROM ooadpj.user WHERE nric = '"+ nric +"'";
		rs = db.readRequest(sql);
		try {
			while(rs.next()){
				NRIC = rs.getString("nric");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.terminate();
		
		if (NRIC == null)
			success = true;
		else
			success = false;
		
		
		return success;
	}
	

}
